import { Util } from '../../utils';

class OverlayBase {
  constructor(options) {
    let {
      id = Util.uuid(),
      payload,
      overlayType,
      style = {},
      hideLabel,
      hideOverlay,
      label,
      labelStyle = {},
    } = options;
    this._isShow = true;
    this._id = id;
    this._isShowLabel = true;
    this._visible = true;
    this._visibleLabel = true;
    this._payload = payload;
    this._overlayType = overlayType;
    this._options = options;
    if (hideOverlay) {
      this._isShow = false;
      this._isShowLabel = false;
      this._visible = false;
      this._visibleLabel = false;
    }
    if (hideLabel) {
      this._isShowLabel = false;
      this._visibleLabel = false;
    }
    this._style = style;
    // 通用label显示
    if (label) {
      this._label = label;
      this._labelStyle = labelStyle;
    }

    this._events = {};

    // L.extend(this, L.Evented.prototype);
  }
  get isShowLabel() {
    return this._isShowLabel;
  }

  get isShow() {
    return this._isShow;
  }

  get visible() {
    return this._visible;
  }

  get style() {
    return this._style;
  }
  get payload() {
    return this._payload;
  }
  get id() {
    return this._id;
  }
  get labelStyle() {
    return this._labelStyle;
  }

  get overlayType() {
    return this._overlayType;
  }

  set visible(value) {
    if (val && this._isShow) {
      // todo
    } else {
    }
    this._visible = value;
    this._visibleLabel = value;
  }

  // 添加
  _onAdd(layer) {
    this.groupId = layer._groupId;
    this.layerId = layer.id;
    this.parent = layer;
    if (this._lft) {
      this._lft.groupId = layer._groupId;
      this._lft.layerId = layer.id;
      this._lft.overlayId = this.id;
    }
    if (this._label && this._overlayType !== 'CanvasMarker') {
      // 初始化lftlabel实例
      this._lftLabel = new DX5S.LFTOverlay['CustomLabel'](this._options);
      this._lftLabel.groupId = layer._groupId;
      this._lftLabel.layerId = layer.id;
      this._lftLabel.overlayId = this.id;
    }
  }
  // 移除
  _onRemove() {
    if (this._viewer) {
      this._viewer.removeOverlay(this._lft);
    }
  }
  showLabel() {
    this._isShowLabel = true;
    this._visibleLabel = true;
    if (this._lftLabel) {
      this._lftLabel.show();
    }
  }

  hideLabel() {
    this._isShowLabel = false;
    this._visibleLabel = false;
  }

  // 展示
  show() {
    this._isShow = true;
    this._visible = true;
    if (this._lft) {
      this._lft.visible = true;
      this._lft.setStyle({ ...this._style });
    }
    if (this._lftLabel) {
      // 直接设置透明度为0 绘制效率更高
      this._lft.setOpacity(this._labelStyle.opacity || 1);
    }
  }
  // 隐藏
  hide() {
    this._isShow = false;
    this._visible = false;
    if (this._lft) {
      this._lft.visible = false;
      // 直接设置透明度为0 绘制效率更高
      this._lft.setStyle({ ...this._style, opacity: 0 });
    }
    if (this._lftLabel) {
      // 直接设置透明度为0 绘制效率更高
      this._lft.setOpacity(0);
    }
  }

  // 销毁 remove
  remove() {
    this.parent && this.parent.removeOverlay(this.id);
  }
  //设置样式 setstyle
  setStyle(options = {}) {
    if (!options instanceof Object) {
      return;
    }
    for (const key in options) {
      const value = options[key];
      if (this._lft) {
        // todo
      }
    }
    this._style = options;
    if (this._lft) {
      this._lft.setStyle(options);
    }
  }

  // 设置事件
  on() {
    L.Evented.prototype.on.apply(this, arguments);
  }
}

export default OverlayBase;
